package com.zb.goods.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.entity.*;
import com.zb.goods.service.ConGoodsService;
import com.zb.service.*;
import com.zb.vo.GoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author WEN
 * @Date 2023/9/12 0012
 **/
@Service
public class ConGoodsServiceImpl implements ConGoodsService {
    @Resource
    private ZjCodeService codeService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjProductSpecService productSpecService;
    @Resource
    private ZjSpareService spareService;
    @Resource
    private ZjSpareSpecInfoService spareSpecInfoService;
    @Resource
    private ZjWorkSpareMaterialService workSpareMaterialService;
    @Resource
    private ZjWorkGoodsService workGoodsService;
    @Resource
    private ZjStoreGoodsStockService storeGoodsStockService;

    @Override
    @Transactional
    public void save(AccounLoginInfo loginInfo, Integer type, GoodsVo goodsVo) {
        if (1 == type) {
            if (null != goodsVo.getId() && CollectionUtil.isNotEmpty(goodsVo.getDelSepcIds())) {
                List<Long> productSpecIds = codeService.getGoodsSpecId(1, goodsVo.getId(), goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(productSpecIds)) {
                    List<ZjProductSpec> specList = productSpecService.getByIds(productSpecIds.stream().distinct().toList());
                    Assert.isTrue(false, "规格名称 [" + specList.stream().map(ZjProductSpec::getName).collect(Collectors.joining("、")) + "] 已与编码关联，不可删除！");
                }
                // 关联工单
                List<ZjWorkGoods> goodsList = workGoodsService.getRelationByGoodsId(goodsVo.getId(), goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(goodsList)) {
                    List<ZjProductSpec> specList = productSpecService.getByIds(goodsList.stream().map(ZjWorkGoods::getGoodsSpecId).distinct().toList());
                    Assert.isTrue(false, "规格名称 [" + specList.stream().map(ZjProductSpec::getName).collect(Collectors.joining("、")) + "] 已与工单商品关联，不可删除！");
                }
                // todo 关联返厂
                // 查询是否关联仓库物料库存
                productSpecIds = storeGoodsStockService.getGoodsSpecIdByGoodsIdAndGoodSpecIds(type, goodsVo.getId(), goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(productSpecIds)) {
                    List<ZjProductSpec> specList = productSpecService.getByIds(productSpecIds);
                    Assert.isTrue(false, "规格名称 [" + specList.stream().map(ZjProductSpec::getName).collect(Collectors.joining("、")) + "] 已与仓库库存商品关联，不可删除！");
                }
            }
            productService.save(loginInfo, goodsVo);
            return;
        }
        if (2 == type) {
            if (null != goodsVo.getId() && CollectionUtil.isNotEmpty(goodsVo.getDelSepcIds())) {
                List<Long> spareSpecIds = codeService.getGoodsSpecId(type, goodsVo.getId(), goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(spareSpecIds)) {
                    List<ZjSpareSpecInfo> specInfos = spareSpecInfoService.getByIds(spareSpecIds.stream().distinct().toList());
                    Assert.isTrue(false, "规格名称[" + specInfos.stream().map(ZjSpareSpecInfo::getName).collect(Collectors.joining("、")) + "]已与编码关联，不可删除！");
                }
                // 关联工单
                spareSpecIds = workSpareMaterialService.getSpecIdsByDelSpecId(goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(spareSpecIds)) {
                    List<ZjSpareSpecInfo> specInfos = spareSpecInfoService.getByIds(spareSpecIds);
                    Assert.isTrue(false, "规格名称[" + specInfos.stream().map(ZjSpareSpecInfo::getName).collect(Collectors.joining("、")) + "]已与工单备件物料关联，不可删除！");
                }
                // todo 关联返厂
                // 查询是否关联仓库物料库存
                spareSpecIds = storeGoodsStockService.getGoodsSpecIdByGoodsIdAndGoodSpecIds(type, goodsVo.getId(), goodsVo.getDelSepcIds());
                if (CollectionUtil.isNotEmpty(spareSpecIds)) {
                    List<ZjSpareSpecInfo> specList = spareSpecInfoService.getByIds(spareSpecIds);
                    Assert.isTrue(false, "规格名称 [" + specList.stream().map(ZjSpareSpecInfo::getName).collect(Collectors.joining("、")) + "] 已与仓库库存商品关联，不可删除！");
                }
            }
            spareService.save(loginInfo, goodsVo);
        }
    }

    // 批量删除商品
    @Override
    @Transactional
    public void deleteByIds(Integer type, List<Long> ids) {
        if (1 == type) {
            // 关联编码
            List<Long> goodsIds = codeService.getGoodsId(type, ids);
            if (!goodsIds.isEmpty()) {
                productExceptionMessage(goodsIds, "编码");
            }
            // 工单商品
            List<ZjWorkGoods> workGoods = workGoodsService.getRelationByGoodsIds(ids);
            if (CollectionUtil.isNotEmpty(workGoods)) {
                productExceptionMessage(workGoods.stream().map(ZjWorkGoods::getGoodsId).distinct().toList(), "工单商品");
            }
            // todo 关联返厂
            // 查询是否关联仓库物料库存
            goodsIds = storeGoodsStockService.getGoodsIdByGoodsIds(type, ids);
            if (CollectionUtil.isNotEmpty(goodsIds)) {
                productExceptionMessage(goodsIds, "仓库库存商品");
            }
            productService.deleteBatchById(ids);
            return;
        }
        if (2 == type) {
            // 关联编码
            List<Long> goodsIds = codeService.getGoodsId(type, ids);
            if (!goodsIds.isEmpty()) {
                spareExceptionMessage(goodsIds, "编码");
            }
            // 关联工单备件
            List<ZjWorkSpareMaterial> materialList = workSpareMaterialService.getRelationBySpareIds(ids);
            if (CollectionUtil.isNotEmpty(materialList)) {
                spareExceptionMessage(materialList.stream().map(ZjWorkSpareMaterial::getSpareId).distinct().toList(), "工单备件物料");
            }
            // todo 关联返厂
            // 查询是否关联仓库物料库存
            goodsIds = storeGoodsStockService.getGoodsIdByGoodsIds(type, ids);
            if (CollectionUtil.isNotEmpty(goodsIds)) {
                spareExceptionMessage(goodsIds, "仓库库存商品");
            }
            spareService.deleteBatchById(ids);
        }
    }

    private void spareExceptionMessage(List<Long> ids, String message) {
        QueryWrapper<ZjSpare> wrapper = new QueryWrapper<>();
        wrapper.select("model");
        wrapper.in("id", ids);
        List<ZjSpare> list = spareService.list(wrapper);
        Assert.isTrue(list.isEmpty(), "备件型号[" + list.stream().map(ZjSpare::getModel).collect(Collectors.joining("、")) + "]已与"+ message +"关联，不可删除！");
    }

    private void productExceptionMessage(List<Long> ids, String message) {
        QueryWrapper<ZjProduct> wrapper = new QueryWrapper<>();
        wrapper.select("model");
        wrapper.in("id", ids);
        List<ZjProduct> list = productService.list(wrapper);
        Assert.isTrue(list.isEmpty(), "产品型号[" + list.stream().map(ZjProduct::getModel).collect(Collectors.joining("、")) + "]已与" + message + "关联，不可删除！");
    }
}
